//
//  MainVC.m
//  OCProject
//
//  Created by 王明钦 on 2018/2/9.
//  Copyright © 2018年 oc. All rights reserved.
//

#import "MainVC.h"
#import "Tab1CV.h"
#import "Tab2CV.h"
#import "Tab3CV.h"
#import "Tab4CV.h"
#import "BaseNavigationController.h"

@interface MainVC ()<UITabBarControllerDelegate>

@end

@implementation MainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addChildViewControllers];
    [self addTabBarItems];

    self.delegate = self;
    
}

-(void)addTabBarItems{
    Tab1CV *tab1 = [[Tab1CV alloc] init];
    BaseNavigationController *tab1Nav = [[BaseNavigationController alloc] initWithRootViewController:tab1];
    
    Tab2CV *tab2 = [[Tab2CV alloc] init];
    BaseNavigationController *tab2Nav = [[BaseNavigationController alloc] initWithRootViewController:tab2];
    
    Tab3CV *tab3 = [[Tab3CV alloc] init];
    BaseNavigationController *tab3Nav = [[BaseNavigationController alloc] initWithRootViewController:tab3];
    
    Tab4CV *tab4 = [[Tab4CV alloc] init];
    BaseNavigationController *tab4Nav = [[BaseNavigationController alloc] initWithRootViewController:tab4];
    
    self.viewControllers = @[tab1Nav,
                             tab2Nav,
                             tab3Nav,
                             tab4Nav
                             ];
}

-(void)addChildViewControllers{
    NSDictionary *dict1 = @{
                            CYLTabBarItemTitle : @"Tab1",
                            CYLTabBarItemImage : @"home_normal",  /* NSString and UIImage are supported*/
                            CYLTabBarItemSelectedImage : @"home_highlight", /* NSString and UIImage are supported*/
                            
                            };
    
    NSDictionary *dict2 = @{
                            CYLTabBarItemTitle : @"Tab2",
                            CYLTabBarItemImage : @"home_normal",  /* NSString and UIImage are supported*/
                            CYLTabBarItemSelectedImage : @"home_highlight", /* NSString and UIImage are supported*/
                            
                            };

    NSDictionary *dict3 = @{
                            CYLTabBarItemTitle : @"Tab3",
                            CYLTabBarItemImage : @"home_normal",  /* NSString and UIImage are supported*/
                            CYLTabBarItemSelectedImage : @"home_highlight", /* NSString and UIImage are supported*/
                            
                            };
    
    NSDictionary *dict4 = @{
                            CYLTabBarItemTitle : @"Tab4",
                            CYLTabBarItemImage : @"home_normal",  /* NSString and UIImage are supported*/
                            CYLTabBarItemSelectedImage : @"home_highlight", /* NSString and UIImage are supported*/
                            
                            };
    
    self.tabBarItemsAttributes = @[dict1, dict2, dict3, dict4];
    
    [self customizeTabBarAppearance:self.cyl_tabBarController];

}

-(void)customizeTabBarAppearance: (CYLTabBarController *) tabBarController{
#warning CUSTOMIZE YOUR TABBAR APPEARANCE
    // Customize UITabBar height
    // 自定义 TabBar 高度
    tabBarController.tabBarHeight = CYL_IS_IPHONE_X ? 65 : 45;
    
    // set the text color for unselected state
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    
    // set the text color for selected state
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
    
    
    // set the text Attributes
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    //设置文字大小
    [tabBar setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} forState:UIControlStateNormal];
    
    // Set the dark color to selected tab (the dimmed background)
    // TabBarItem选中后的背景颜色
    // [self customizeTabBarSelectionIndicatorImage];
    
    // update TabBar when TabBarItem width did update
    // If your app need support UIDeviceOrientationLandscapeLeft or UIDeviceOrientationLandscapeRight，
    // remove the comment '//'
    // 如果你的App需要支持横竖屏，请使用该方法移除注释 '//'
    // [self updateTabBarCustomizationWhenTabBarItemWidthDidUpdate];
    
    // set the bar shadow image
    // This shadow image attribute is ignored if the tab bar does not also have a custom background image.So at least set somthing.
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    //    [[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"tapbar_top_line"]];
    
    // set the bar background image
    // 设置背景图片
    UITabBar *tabBarAppearance = [UITabBar appearance];
    
    //FIXED: #196
//    UIImage *tabBarBackgroundImage = [UIImage imageNamed:@"tab_bar"];
//    UIImage *scanedTabBarBackgroundImage = [[self class] scaleImage:tabBarBackgroundImage toScale:1.0];
    //     [tabBarAppearance setBackgroundImage:scanedTabBarBackgroundImage];
    
    // remove the bar system shadow image
    // 去除 TabBar 自带的顶部阴影
    // iOS10 后 需要使用 `-[CYLTabBarController hideTabBadgeBackgroundSeparator]` 见 AppDelegate 类中的演示;
    //    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    
}

//点击事件
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
